<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class m_post extends CI_Model {

    var $id_post;
    var $id_user;
    var $id_location;
    var $id_category;
    var $post_title;
    var $post_date;
    var $post_content;
    var $post_lati;
    var $post_longi;
    var $post_status;
    var $post_thumb;

    public function __construct() {
        parent::__construct();
        if(isset($_COOKIE['c_iduser'])){
            $this->id_user = $_COOKIE['c_iduser'];
        }
        $this->post_status = "N";
    }

    function set_id_location($id_location) {
        $this->id_location = $id_location;
    }

    function set_id_category($id_category) {
        $this->id_category = $id_category;
    }

    function set_post_title($post_title) {
        $this->post_title = $post_title;
    }

    function set_post_content($post_content) {
        $this->post_content = $post_content;
    }

    function set_post_lati($post_lati) {
        $this->post_lati = $post_lati;
    }

    function set_post_longi($post_longi) {
        $this->post_longis = $post_longi;
    }

    function set_post_thumb($post_thumb) {
        $this->post_thumb = $post_thumb;
    }

    public function get_post_page($num, $offset){
        $sql = "SELECT * FROM post p "
                . "JOIN category c ON p.id_category = c.id_category "
                . "JOIN location l ON p.id_location = l.id_location "
                . "JOIN user u ON p.id_user = u.id_user "
                . "ORDER BY post_date  DESC LIMIT $offset, $num";
        $data = $this->db->query($sql);
        $result = $data->result_array();

        return $result;
    }

    function select_all_post() {
        $sql = "SELECT * FROM post p "
                . "JOIN category c ON p.id_category = c.id_category "
                . "JOIN location l ON p.id_location = l.id_location "
                . "JOIN user u ON p.id_user = u.id_user "
                . "ORDER BY post_date    DESC";
        $query = $this->db->query($sql);
        $result = $query->result_array();
        if($query){
            return $result;
        }else{
            return FALSE;
        }
    }
    function insert_post($param) {
        $sql = "INSERT INTO post(id_user, post_title, post_content, id_category, id_location, latitude, longitude, thumbnail, post_date)
                VALUES($this->id_user,?,?,?,?,?,?,?, now())";
        $query = $this->db->query($sql,$param);
        if($query){
            return TRUE;
        }else{
            return FALSE;
        }
        
    }
    function select_post_byId($param) {
        $sql = "SELECT * From post p
                JOIN location l ON p.id_location = l.id_location
                JOIN user u ON p.id_user = u.id_user
                JOIN category c ON p.id_category = c.id_category
                WHERE id_post = ?";
        $query = $this->db->query($sql, $param);
        
        if($query->num_rows()>0){
            $result = $query->row_array();
            return $result;
        }else{
            return FALSE;
        }
    }
    
    function select_post_by_user($num, $offset) {
        $sql = "select * from post p "
                ."JOIN user u ON u.id_user = p.id_user "
                ."JOIN category c ON c.id_category =  p.id_category "
                ."JOIN location l ON l.id_location =  p.id_location "
                ."where p.id_user = $_COOKIE[c_iduser] "
                ."ORDER BY post_date DESC LIMIT $offset, $num";
        //echo $num." ".$offset." ".$_COOKIE['c_iduser'];
        $query = $this->db->query($sql);
        
        if($query){
            $result = $query->result_array();
            $query->free_result();
            return $result;
        }else{
            return false;
        }
    }
    function select_post_by_location($param, $num, $offset) {
        $sql = "select * from post p 
                JOIN location l ON p.id_location = l.id_location
                JOIN user u ON p.id_user = u.id_user
                JOIN category c ON p.id_category = c.id_category
                WHERE l.location_name LIKE ?
                ORDER BY p.post_date DESC LIMIT $offset, $num
                ";
        $query = $this->db->query($sql, $param);
        
        if($query){
            $result = $query->result_array();
            $query->free_result();
            return $result;
        }else{
            return false;
        }
    }
    
    function select_by_category($param) {
        $sql = "select * from post p 
                JOIN location l ON p.id_location= l.id_location
                JOIN user u ON p.id_user = u.id_user
                WHERE id_category = ?
                LIMIT 5";
        $query = $this->db->query($sql, $param);
        
        if($query){
            $result = $query->result_array();
            return $result;
        }else{
            return false;
        }
    }
    

    function update_post($param){
        if(!isset($param['post_thumb'])):
            $sql = "UPDATE post SET post_title = ?, post_content = ?, id_category = ?, id_location = ?, latitude = ?, longitude = ? WHERE id_post = ?";
        else:
            $sql = "UPDATE post SET post_title = ?, post_content = ?, id_category = ?, id_location = ?, latitude = ?, longitude = ?, thumbnail = ? WHERE id_post = ?";
        endif;
        $query = $this->db->query($sql, $param);

        if ($query) {
            return TRUE;
        } else {
            return false;
        }
    }

    function delete_post($param){
        $sql = "DELETE FROM post where id_post =  ?";
        $query =  $this->db->query($sql, $param);

        if ($query) {
            return TRUE;
        } else {
            return false;
        }   
    }
}
